package oakclub.entity;

import java.util.HashMap;
import java.util.Map;

public class LanguageEntity extends BaseEntity
{
    private int id;

    public enum Language
    {
        UNKNOWN(-1, "", "-"),
        VI(0, "vi", "Vietnamese"), EN(1, "en", "English"), ID(2, "id", "Indonesian"),  TH(3, "th", "Thai"),
        ES(4, "es", "Spanish"), FR(5, "fr", "French"), DE(6, "de", "German"), TR(7, "tr", "Turkish");

        private final int id;
        private final String code;
        private final String name;

        private Language(int id, String code, String name)
        {
            this.id = id;
            this.code = code;
            this.name = name;
        }

        public int getId()
        {
            return id;
        }

        public String getCode()
        {
            return code;
        }

        public String getName()
        {
            return name;
        }

        private static final Map<Integer, Language> idLookup = new HashMap<>();
        private static final Map<String, Language> nameLookup = new HashMap<>();

        static
        {
            for(Language language : Language.values())
            {
                idLookup.put(language.id, language);
                nameLookup.put(language.name, language);
            }
        }

        public static String getCode(int id)
        {
            Language language = idLookup.get(id);
            if(language == null)
            {
                return UNKNOWN.getCode();
            }

            return language.getCode();
        }

        public static String getName(int id)
        {
            Language language = idLookup.get(id);
            if(language == null)
            {
                return UNKNOWN.getName();
            }

            return language.getName();
        }

        public static int getId(String name)
        {
            if(name == null || name.isEmpty())
            {
                return UNKNOWN.getId();
            }

            Language language = nameLookup.get(name);
            if(language == null)
            {
                return UNKNOWN.getId();
            }

            return language.getId();
        }
    }

    public LanguageEntity()
    {

    }

    public LanguageEntity(int id)
    {
        this.id = id;
    }

    public LanguageEntity(String name)
    {
        this.id = Language.getId(name);
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getCode()
    {
        return Language.getCode(this.id);
    }

    public String getName()
    {
        return Language.getName(this.id);
    }
}
